package club.gskj.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import club.gskj.gulimall.ware.entity.PurchaseDetailEntity;
import club.gskj.gulimall.ware.vo.PurchaseDoneVo;
import club.gskj.gulimall.ware.vo.PurchaseMergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import club.gskj.gulimall.ware.entity.PurchaseEntity;
import club.gskj.gulimall.ware.service.PurchaseService;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.R;


/**
 * 采购信息
 *
 * @author 康纳-connor
 * @email 2417039231@gmail.com
 * @date 2021-02-07 10:08:31
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;


    /**
     * @param purchaseDoneVo
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 15:06
     * @Description 工作人员提交完成 采购需求
     */
    @PostMapping("/done")
    public R donePurchase(@RequestBody PurchaseDoneVo purchaseDoneVo) {

        purchaseService.donePurchase(purchaseDoneVo);
        return R.ok();
    }


    /**
     * @param ids
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 13:01
     * @Description 工作人员领取采购单
     */
    @PostMapping("/received")
    public R receivedPurchase(@RequestBody List<Long> ids) {

        purchaseService.receivedPurchase(ids);
        return R.ok();
    }


    /***
     *
     * @param purchaseId
     * @param items
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 11:32
     * @Description 合并采购单信息
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody PurchaseMergeVo purchaseMergeVo) {
        // 合并采购单
        purchaseService.mergePurchase(purchaseMergeVo);
        return R.ok();
    }


    /**
     * @param params
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 11:22
     * @Description 查询所有新建、已分配的采购单列表
     */
    @RequestMapping("/unreceive/list")
    public R Listunreceive(@RequestParam Map<String, Object> params) {

        PageUtils page = purchaseService.queryPageUnreceive(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
